package org.sninwo.lnbook.book.web;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sninwo.lnbook.base.BaseAction;
import org.sninwo.lnbook.base.Page;
import org.sninwo.lnbook.book.entity.Book;
import org.sninwo.lnbook.book.entity.BookType;
import org.sninwo.lnbook.book.service.BookManager;
import org.sninwo.lnbook.book.service.PhotoManager;
import org.sninwo.lnbook.book.service.SearchManager;
import org.sninwo.lnbook.uitls.WebUtils;

@Namespace("/")
public class ViewAction extends BaseAction{
	public static int DEFAULT_PAGE_SIZE = 15;
	
	Logger logger = LoggerFactory.getLogger(ViewAction.class);
	private static final long serialVersionUID = -170561663455733371L;

	public String words;
	public Page<Map<String, Object>> page = new Page<Map<String, Object>>(DEFAULT_PAGE_SIZE);
	public double searchTime;
	
	public Long bookId;
	public Book book = new Book();
	public Long bookTypeId;
	public BookType bookType = new BookType();
	public List<Book> books = new ArrayList<Book>();
	public List<BookType> bookTypes = new ArrayList<BookType>();
	
	public String bookGuide;
	
	public String photoName;
	public List<Map<String, Object>> photoFolders = new ArrayList<Map<String, Object>>();
	public int pfIdx; //当前图片目录序号
	
	@Resource SearchManager searchMgr;
	@Resource BookManager bookMgr;
	@Resource PhotoManager photoMgr;
	
	/**
	 * 执行搜索的方法
	 */
	public String search() throws Exception{
		if (StringUtils.isNotBlank(words)){
			words = words.trim();
			long start = System.currentTimeMillis();
			page = searchMgr.searchBook(page, words.split("\\s+"));
			searchTime = (System.currentTimeMillis() - start) / 1000.0;
			logger.debug("搜索书籍耗时{}毫秒", System.currentTimeMillis() - start);
		}
		return "search";
	}
	
	/**
	 * 显示一本书
	 * @return
	 */
	public String bookDetail(){
		if (bookId > 0){
			book = bookMgr.getBook(bookId);
			if (book != null){
				bookType = bookMgr.getBookType(book.getTypeId());
			}
		}
		return "book-detail";
	}
	
	/**
	 * 显示总目，一次只显示一种类别的书籍
	 * @return
	 */
	public String showCatalogue(){
		bookTypes = bookMgr.getAllBookTypes();
		if (!bookTypes.isEmpty()){
			if (bookTypeId == null) bookTypeId = bookTypes.get(0).getId();
			books = bookMgr.findAllBooksByTypeId(bookTypeId);
		}
		return "catalogue";
	}
	
	/**
	 * 获取书籍内容中引用的书籍。
	 */
	public String findBookRefers(){
		if (bookId == null || bookId == 0){
			return json();
		}
		List<Book> referBooks = searchMgr.findBookRefers(bookId);
		return json(referBooks);
	}
	
	/**
	 * 凡例
	 */
	public String bookGuide() throws Exception{
		File file = new File(WebUtils.getRealPath(BookAction.BOOK_GUIDE_FILE_PATH));
		if (file.exists()){
			bookGuide = FileUtils.readFileToString(file, "utf-8");
		}
		return "book-guide";
	}
	
	/**
	 * 使用说明
	 */
	public String bookUseage(){
		return "book-useage";
	}
	
	/**
	 * 图片库
	 */
	public String bookPhotos(){
		photoFolders = photoMgr.getFoldersToShow();
		if (page.getPageSize() == DEFAULT_PAGE_SIZE){
			page.setPageSize(18);
		}
		page = photoMgr.getPageListInFolder(page, pfIdx);
		return "book-photos";
	}
	
	/**
	 * 显示图片的原图
	 */
	public String showImage() throws Exception{
		return stream(photoMgr.getPhoto(pfIdx, photoName));
	}
	
	/**
	 * 显示图片的缩略图。
	 */
	public String showMinImage() throws Exception{
		return stream(photoMgr.getMinPhoto(pfIdx, photoName));
	}
}
